﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using PetaPoco;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [AppService]
    public class ArticleExamineService
    {
        private readonly ArticleExamineRepository _articleExamineRepository;

        public ArticleExamineService(ArticleExamineRepository articleExamineRepository)
        {
            _articleExamineRepository = articleExamineRepository;
        }

        /// <summary>
        /// 统计当前用户等审核文件数
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<long> CountExamineAsync(int userId)
        {
            return await _articleExamineRepository.CountExamineAsync(userId);
        }

        /// <summary>
        /// 审核文件列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Page<ViewArticleExamine>> GetListAsync(ArticleExamineQuery query)
        {
            return await _articleExamineRepository.GetListAsync(query);
        }

        /// <summary>
        /// 查询要编辑的一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewExamine> GetEditByAidAsync(int id)
        {
            ViewExamine examine = await _articleExamineRepository.GetEditByAidAsync(id);
            return examine;
        }

        /// <summary>
        /// 查询要编辑的一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewExamine> GetEditByIdAsync(int id)
        {
            ViewExamine examine = await _articleExamineRepository.GetEditByIdAsync(id);
            return examine;
        }

        /// <summary>
        /// 查询要编辑的一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewArtExamine> GetArtAsync(int aid)
        {
            ViewArtExamine article = await _articleExamineRepository.GetEditArticleAsync(aid);
            return article;
        }
        public async Task<ViewArtExamine> GetArtNoContentAsync(int aid)
        {
            ViewArtExamine article = await _articleExamineRepository.GetArtNoContentAsync(aid);
            return article;
        }

        /// <summary>
        /// 查询当前文件流转列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<ViewArticleExamine>> GetExamineingAsync(int aid)
        {
            List<ViewArticleExamine> article = await _articleExamineRepository.GetExamineingAsync(aid);
            return article;
        }

        /// <summary>
        /// 插入或更新
        /// </summary>
        /// <returns></returns>
        public virtual async Task<int> InsertOrUpdateAsync(ViewArticleExamine articleExamine, UserAction userInfo)
        {
            if (articleExamine.UserId < 1)
                return 402;
            if (articleExamine.UserId == userInfo.UserId)
                return 404;
            return await _articleExamineRepository.InsertOrUpdateAsync(articleExamine);
        }
        /// <summary>
        /// 审核一个文件
        /// </summary>
        /// <returns></returns>
        public virtual async Task<int> EditPostAsync(ArticleExamine articleExamine)
        {
            return await _articleExamineRepository.EditPostAsync(articleExamine);
        }
    }
}
